const app = new getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    prompt: '',
    default_img: {},
    goods_list: [],
    //轮播图属性
    adv_img: [],
    autoplay: true,
    interval: 3000,
    duration: 1000,
    circular: true,
    indicator_color: '#AAA',
    indicator_active_color: '#FFF',
    swiper_height: 150,
    goodsDetailFlag: 0,
    next_page: 2,
    integral_help: 0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    let that = this;

    app.sendRequest({
      url: 'System.Shop.advDetail',
      data: {
        ap_keyword: 'APPLET_POINT_SWIPER',
        export_type: 'data'
      },
      success: function(res) {
        if (res.code == 0) {
          let data = res.data;
          data.advs = app.checkEmpty(data.advs, []);
          for (let index in data.advs) {
            data.advs[index].adv_image = app.IMG(data.advs[index].adv_image);
            if (index == 0) {
              app.getWindowSize(that, true, data.advs[index].adv_image);
            }
          }

          that.setData({
            adv_img: data.advs,
          })
        }
      }
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    let that = this;
    app.restStatus(that, 'goodsDetailFlag');
    app.defaultImg(that, true);
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {
    let that = this;
    let goods_list = that.data.goods_list;
    let next_page = that.data.next_page;
    let list_length = goods_list.length;
    list_length = list_length - 1 < 0 ? 0 : list_length - 1;

    let condition = {
      'ng.state': 1,
      'ng.point_exchange_type': ['neq', 0],
    };
    app.sendRequest({
      url: 'System.Goods.goodsList',
      data: {
        page_index: next_page,
        condition: JSON.stringify(condition),
        order: 'sort desc,create_time desc'
      },
      success: function(res) {
        let code = res.code;
        let data = res.data;
        if (code == 0) {
          let new_goods_list = data.data;
          if (app.checkArray(new_goods_list)) {
            if (new_goods_list.length > 0) {
              next_page++;
            }

            let d = {};
            let key = '';

            for (let index in new_goods_list.data) {
              let length = parseInt(list_length) + parseInt(index);
              let img = new_goods_list.data[index].pic_cover_small;
              new_goods_list.data[index].pic_cover_small = app.IMG(img);
              key = 'goods_list[' + length + ']';
              d[key] = new_goods_list.data[index];
            }
            key = 'next_page';
            d[key] = next_pag;
            that.setData(d);
          }
        }
      }
    });
  },

  loadInfo: function() {
    let that = this;
    let condition = {
      'ng.state': 1,
      'ng.point_exchange_type': ['neq', 0],
    };

    app.sendRequest({
      url: 'System.Goods.goodsList',
      data: {
        page_index: 1,
        condition: JSON.stringify(condition),
        order: 'sort desc,create_time desc'
      },
      success: function(res) {
        let code = res.code;
        let data = res.data;
        console.log(res);
        if (code == 0) {
          let goods_list = data.data;

          if (app.checkArray(goods_list)) {
            for (let index in goods_list) {
              let img = goods_list[index].pic_cover_small;
              goods_list[index].pic_cover_small = app.IMG(img);
            }

            that.setData({
              goods_list: goods_list,
            })
          }
        }
      }
    });
    that.loadMemberInfo();
  },

  loadMemberInfo: function() {
    let that = this;
    let token = app.globalData.token;
    let is_login = app.globalData.is_login;
    if (app.checkEmpty(token, '') != '' && is_login == 1) {
      // 会员信息
      app.sendRequest({
        url: 'System.Member.memberInfo',
        data: {},
        success: function (res) {
          let code = res.code;
          let data = res.data;
          if (code == 0) {
            let member_info = data;
            let img = member_info.user_info.user_headimg;
            member_info.user_info.user_headimg = app.IMG(img); //图片路径处理

            that.setData({
              member_info: res.data,
            })
          }
        }
      })
      // 会员账户信息
      app.sendRequest({
        url: 'System.Member.memberAccount',
        data: {},
        success: function(res) {
          let code = res.code;
          let data = res.data;
          if (code == 0) {
            let member_account = data;
            that.setData({
              member_account: member_account,
            })
          }
        }
      })
    }
  },

  /**
   * 积分说明
   */
  integralHelp: function () {
    let animation = wx.createAnimation({
      duration: 400,
    })
    animation.bottom('0').step();
    this.animation = animation;
    this.setData({
      integral_help: 1,
      animation: this.animation.export()
    })
  },

  /**
   * 头像加载失败
   */
  errorHeadImg: function (e) {
    let that = this;
    let member_info = that.data.member_info;
    let default_img = that.data.default_img;
    let img = member_info.user_info.user_headimg;
    let parm = {};
    let parm_key = "member_info.user_info.user_headimg";

    if (default_img.is_use == 1) {
      default_img = default_img.value.default_headimg;
      if (img.indexOf(default_img) == -1) {
        parm[parm_key] = default_img;
        that.setData(parm);
      }
    }
  },

  /**
   * 图片加载获取高度
   */
  imgLoad: function (e) {
    let res = wx.getSystemInfoSync();
    let height = e.detail.height;
    let width = e.detail.width;
    let rate = width / height;
    let swiper_height = res.windowWidth / rate;

    this.setData({
      swiperHeight: swiper_height
    })
  },

  /**
   * 图片加载失败
   */
  errorImg: function (e) {
    let that = this;
    let index = e.currentTarget.dataset.index;
    let goods_list = that.data.goods_list;
    let default_img = that.data.default_img;
    let base = that.data.Base;
    let parm = {};
    let img = goods_list[index].pic_cover_small;

    if (default_img.is_use == 1) {
      default_img = default_img.value.default_goods_img;
      if (img.indexOf(default_img) != 1) {
        let parm_key = "goods_list[" + index + "].pic_cover_small";

        parm[parm_key] = default_img;
        that.setData(parm);
      }
    }
  },

  /**
   * 关闭弹框
   */
  closeLayer: function () {
    let animation = wx.createAnimation();
    animation.bottom('-100%').step();
    this.animation = animation;

    this.setData({
      animation: this.animation.export(),
      integral_help: 0
    })
  },

  /**
   * 链接跳转
   */
  linkJump: function (e) {
    let url = e.currentTarget.dataset.url;
    app.linkJumpDetection(url);
  }
})